//
//  MTCommentController.m
//  07-美团外卖
//
//  Created by Apple on 16/10/19.
//  Copyright © 2016年 itheima. All rights reserved.
//
//验证码登录
#import "MTCommentController.h"

@interface MTCommentController ()
@property (nonatomic,strong) UITextField *textField;//手机号码框
@property (nonatomic,strong) UITextField *textField1;//验证码号码框
@property (nonatomic,strong) UIButton *btn;//验证码
@property (nonatomic,assign) int iSecond; //短信验证秒数
@property (nonatomic,strong) NSTimer *timer; //短信验证时间
@end

@implementation MTCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    

	
    //self.view.backgroundColor = [UIColor orangeColor];

	UIView *view = [[UIView alloc] init];
	[self.view addSubview:view];
	view.backgroundColor = [UIColor whiteColor];

	[view mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.view);
		make.height.equalTo(@(50));
		make.left.right.equalTo(self.view);
	}];

	UIView *lengView = [[UIView alloc] init];
	[self.view addSubview:lengView];
	[lengView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(view.mas_bottom);
		make.left.right.equalTo(self.view);
		make.height.equalTo(@(0.5));
	}];

	UIImageView *imageView = [[UIImageView alloc] init];
	imageView.image = [UIImage imageNamed:@"输入手机号"];
	[view addSubview:imageView];

	[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view).offset(15);
		make.centerY.equalTo(view);
		make.width.equalTo(@(10));
		make.height.equalTo(@(13));
	}];

	UIButton *btn = [[UIButton alloc] init];
	_btn = btn;
	[self.view addSubview:btn];
	[btn addTarget:self action:@selector(yanzhengma) forControlEvents:UIControlEventTouchUpInside];
	btn.titleLabel.textAlignment = NSTextAlignmentCenter;
	[btn setTitle:@"获取验证码                " forState:UIControlStateNormal];
	[btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
	btn.backgroundColor = [UIColor redColor];
	[btn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(lengView).offset(-30);
		make.centerY.equalTo(view);
		make.width.equalTo(@(100));
	}];

	UITextField *textField = [[UITextField alloc] init];
	_textField = textField;
	textField.placeholder = @"请输入手机号码";
	[self.view addSubview:textField];
	textField.backgroundColor = [UIColor whiteColor];
	[textField mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(imageView.mas_right).offset(15);
		make.centerY.equalTo(view);
		make.right.equalTo(btn.mas_left).offset(-20);
	}];

	UIView *lengView1 = [[UIView alloc] init];
	[self.view addSubview:lengView1];
	lengView1.backgroundColor = [UIColor hm_colorWithR:0 G:0 B:0 alpha:0.3];
	[lengView1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(view.mas_bottom).offset(-1);
		make.left.right.equalTo(view);
		make.height.equalTo(@(0.5));
	}];

	UIView *view1 = [[UIView alloc] init];
	[self.view addSubview:view1];
	view1.backgroundColor = [UIColor whiteColor];

	[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(view.mas_bottom);
		make.height.equalTo(@(50));
		make.left.right.equalTo(self.view);
	}];

	UIImageView *imageView1 = [[UIImageView alloc] init];
	imageView1.image = [UIImage imageNamed:@"验证码"];
	[view1 addSubview:imageView1];

	[imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view).offset(15);
		make.centerY.equalTo(view1);
		make.width.equalTo(@(10));
		make.height.equalTo(@(13));
	}];

	UITextField *textField1 = [[UITextField alloc] init];
	textField1.placeholder = @"请输入验证码";
	_textField1 = textField1;
	[self.view addSubview:textField1];
	textField1.backgroundColor = [UIColor whiteColor];
	[textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(imageView1.mas_right).offset(15);
		make.centerY.equalTo(view1);
		make.right.equalTo(btn).offset(-10);
	}];

	UIView *lengView2 = [[UIView alloc] init];
	[self.view addSubview:lengView2];
	lengView2.backgroundColor = [UIColor hm_colorWithR:0 G:0 B:0 alpha:0.3];
	[lengView2 mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(view1.mas_bottom).offset(-1);
		make.left.right.equalTo(view1);
		make.height.equalTo(@(0.5));
	}];

	UIButton *button = [[UIButton alloc] init];
	//button.layer.borderWidth = 1;
	[button addTarget:self action:@selector(btnSelect:) forControlEvents:UIControlEventTouchUpInside];
	[button setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
	[button setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
	[self.view addSubview:button];
	[button mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(imageView);
		make.top.equalTo(lengView2.mas_bottom).offset(15);
		make.width.height.equalTo(@(15));
	}];

	UILabel *xieyi = [[UILabel alloc] init];
	[self.view addSubview:xieyi];
	xieyi.text = @"同意并接受《救个急协议》";
	xieyi.font = [UIFont systemFontOfSize:12];
	[xieyi mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(button.mas_right).offset(10);
		make.top.equalTo(button);
	}];

	UIButton *redBtn = [[UIButton alloc] init];
	[self.view addSubview:redBtn];
	redBtn.backgroundColor = [UIColor redColor];
	redBtn.layer.cornerRadius = 2;
	[redBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
	[redBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[redBtn setTitle:@"登录" forState:UIControlStateNormal];
	[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(xieyi.mas_bottom).offset(10);
		make.left.equalTo(self.view).offset(15);
		make.right.equalTo(self.view).offset(-15);
		make.height.equalTo(@(50));
	}];
}

-(void)btnSelect:(UIButton *)btn{
	btn.selected = !btn.selected;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)login{
	NSDictionary *dict = @{@"phone":_textField.text,@"code":_textField1.text};
	[[CXNetworkTool sharedTool] postRequestWithURL:@"login" parameters:dict complete:^(id responseObject, NSError *error) {

	}];
}

-(void)yanzhengma{
	_btn.userInteractionEnabled = NO;
	_btn.backgroundColor = [UIColor hm_colorWithHex:0xbfbfbf];
	_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
	[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
	_iSecond = 61;
	NSDictionary *dict = @{@"phone":_textField.text};
	[[CXNetworkTool sharedTool] postRequestWithURL:@"send-sms" parameters:dict complete:^(id responseObject, NSError *error) {
		
	}];
}

- (void)changeTimeAtTimedisplay
{
	_btn.titleLabel.text = [NSString stringWithFormat:@"%ds后重新发送",--_iSecond];
	if(_iSecond == 0){
		_btn.userInteractionEnabled = YES;
		_btn.backgroundColor = [UIColor redColor];
		_btn.titleLabel.text = @"获取验证码";
		_iSecond = 60;
		_timer.fireDate = [NSDate distantFuture];
	}
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
